package com.srs.backend.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.backend.mapper.FoodMapper;
import com.srs.backend.mapper.OrderItemMapper;
import com.srs.backend.pojo.OrderItem;
import com.srs.backend.service.OrderItemService;
import com.srs.backend.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class OrderItemServiceImpl implements OrderItemService {
    @Resource
    private OrderItemMapper orderItemMapper;
    @Resource
    private FoodMapper foodMapper;
    @Override
    public Result add(Map<String, String> data) {
        String orderIdStr = data.get("order_id");
        Integer orderId;
        String foodIdStr = data.get("food_id");
        Integer foodId;
        String chefIdStr = data.get("chef_id");
        Integer chefId;
        String remark = data.get("remark");

        try {
            orderId = Integer.parseInt(orderIdStr);
            foodId = Integer.parseInt(foodIdStr);
            chefId= Integer.parseInt(chefIdStr);
        } catch (NumberFormatException e) {
            return Result.error("数据类型错误");
        }

        orderItemMapper.insert(new OrderItem(null, orderId, foodId, chefId ,remark, null, null));

        return Result.ok("success");
    }

    @Override
    public Result delete(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        OrderItem curOrderItem = orderItemMapper.selectById(id);
        if (curOrderItem == null) {
            return Result.error("当前菜品类型不存在或已被删除");
        }
        orderItemMapper.deleteById(curOrderItem);
        return Result.ok();
    }

    @Override
    public Result modify(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        OrderItem curOrderItem = orderItemMapper.selectById(id);
        if (curOrderItem == null) {
            return Result.error("当前菜品类型不存在或已被删除");
        }

        QueryWrapper<OrderItem> orderItemQueryWrapper = new QueryWrapper<>();
        orderItemQueryWrapper.eq("id", id);
        List<OrderItem> orderItems = orderItemMapper.selectList(orderItemQueryWrapper);
        if (!orderItems.isEmpty()) {
            return Result.error("当前菜品类型已经存在");
        }

        curOrderItem.setId(id);

        orderItemMapper.updateById(curOrderItem);
        return Result.ok();
    }

    @Override
    public Result getAll() {return Result.ok(orderItemMapper.selectList(null)); }

    // 厨师完成菜品
    @Override
    public Result done(Map<String, String> data) {
        String chefIdStr = data.get("chef_id");
        String orderItemIdStr = data.get("order_item_id");

        Integer chefId, orderItemId;
        try {
            chefId = Integer.parseInt(chefIdStr);
            orderItemId = Integer.parseInt(orderItemIdStr);
        } catch (Exception e) {
            return Result.error("数据类型有误");
        }
        // 判断这个订单项目有没有被完成
        OrderItem orderItem = orderItemMapper.selectById(orderItemId);
        if (orderItem.getChefId() != null) {
            return Result.error("该项目已被完成，厨师id为：" + orderItem.getChefId().toString());
        }

        orderItem.setChefId(chefId);
        orderItem.setFinishTime(new Date());

        orderItemMapper.updateById(orderItem);

        return Result.ok();
    }

    // 根据订单id获取订单对应的items
    @Override
    public Result getOrderItemByOrderId(Map<String, String> data) {
        String orderIdStr = data.get("order_id");
        Integer orderId;
        try {
            orderId = Integer.parseInt(orderIdStr);
        } catch (Exception e) {
            return  Result.error("订单id数据类型有误");
        }
        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        List<OrderItem> orderItems = orderItemMapper.selectList(queryWrapper);
        for (int i = 0; i < orderItems.size(); i ++ ) {
            Integer foodId = orderItems.get(i).getFoodId();
            orderItems.get(i).setRemark(this.getFoodNameByFoodId(foodId));
        }
        return Result.ok(orderItems);
    }

    @Override
    public Result getAllUnfinishedOrderItem() {
//        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("finish_time", null);
//        List<OrderItem> orderItems = orderItemMapper.selectList(queryWrapper);
        List<OrderItem> res = new LinkedList<>();

        List<OrderItem> orderItemList = orderItemMapper.selectList(null);
        for (OrderItem item:
                orderItemList) {
            item.setFoodName(foodMapper.selectById(item.getFoodId()).getName());
            if (item.getChefId() == null) {
                res.add(item);
            }
        }

        return Result.ok(res);
    }

    @Override
    public Result getAllFinishedByChefId(Map<String, String> data) {
        String chefIdStr = data.get("chef_id");
        Integer chefId;
        try {
            chefId = Integer.parseInt(chefIdStr);
        } catch (Exception e) {
            return Result.error("厨师id数据类型有误");
        }
        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chef_id", chefId);
        List<OrderItem> orderItems = orderItemMapper.selectList(queryWrapper);
        for (OrderItem item:
                orderItems) {
            item.setFoodName(foodMapper.selectById(item.getFoodId()).getName());
        }
        return Result.ok(orderItems);
    }

    public String getFoodNameByFoodId(Integer id) {
        return foodMapper.selectById(id).getName();
    }
}
